#include "mprpcapplication.h"
#include <iostream>

#include "unistd.h"

MprpcConfig MprpcApplication::m_config;

MprpcApplication::MprpcApplication()
{
}

void MprpcApplication::Init(int argc, char *argv[])
{
    if (argc < 2)
    {
        std::cout << "\r\n\t Usage: command -i <configFile>\r\n\n";
        exit(1);
    }

    int c;
    std::string config_file;

    // 参数解析
    // "i:" 表示要有-i选项，且必须要有参数
    while ((c = getopt(argc, argv, "i:")) != -1)
    {
        switch (c)
        {
        case 'i':
            config_file = optarg;
            break;
        case '?':
            exit(1);
        case ':':
            // 没有参数，只有选项
            std::cout << "\r\n\tneed a config_file\r\n";
            exit(1);
        default:
            break;
        }
    }

    // 开始加载配置项
    m_config.LoadConfigFile(config_file.c_str());

    // std::cout << "rpcserverip:" << m_config.GetValue("rpcserverip") << std::endl;
    // std::cout << "rpcseverport:" << m_config.GetValue("rpcserverport") << std::endl;
    // std::cout << "zookeeperip:" << m_config.GetValue("zookeeperip") << std::endl;
    // std::cout << "zookeeperport:" << m_config.GetValue("zookeeperport") << std::endl;
}

MprpcApplication &MprpcApplication::GetInstance()
{
    static MprpcApplication app;
    return app;
}

MprpcConfig &MprpcApplication::GetConfig()
{
    return m_config;
}
